package net.yegong.util.reflect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

/**
 * The AbstractClassFactory is a factory which can create new instance of
 * specified abstract class or interface.
 * 
 * @author cooper
 */
public class AbstractObjectFactory {

	private static final List<Class<?>> SUPPORTED_CLAZZ;

	static {
		SUPPORTED_CLAZZ = new ArrayList<Class<?>>();
		SUPPORTED_CLAZZ.add(ArrayList.class);
		SUPPORTED_CLAZZ.add(LinkedList.class);
		SUPPORTED_CLAZZ.add(HashSet.class);
		SUPPORTED_CLAZZ.add(HashMap.class);
		SUPPORTED_CLAZZ.add(Vector.class);
		SUPPORTED_CLAZZ.add(Hashtable.class);
	}

	public static Object newInstanceOf(Class<?> clazz) {
		for (Class<?> cl : SUPPORTED_CLAZZ) {
			if (clazz.isAssignableFrom(cl)) {
				try {
					return cl.newInstance();
				}
				catch (Exception e) {
				}
			}
		}
		throw new RuntimeException("Can't create new instance for [" + clazz.toString() + "]");
	}
}
